package com.corpdata.service;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.corpdata.common.result.DataTable;
import com.corpdata.common.result.Result;
import com.corpdata.common.utils.ResultUtil;
import com.corpdata.dao.OrgUserMapper;
import com.corpdata.entity.OrgUser;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class OrgUserService {

	@Autowired
	private OrgUserMapper orgUserMapper;

	/**
	 * 查询数据列表
	 * 
	 * @param dt
	 * @return
	 */
	public DataTable<OrgUser> getUserByPage(DataTable dt) {
		String userName = "";
		if (dt.getSearchParams() != null) {
			userName = dt.getSearchParams().get("userName").toString();
		}
		// System.out.println(dt.getOrder());
		PageHelper.startPage(dt.getPage(), dt.getSize());
		Page<OrgUser> rows = orgUserMapper.selectAll();
		dt.setTotalAndRows(rows.getTotal(), rows);
		return dt;
	}

	/**
	 * 新增数据
	 * 
	 * @param user
	 * @return
	 */
	public Result create(OrgUser user) {
		orgUserMapper.create(user);
		return ResultUtil.success();
	}

	/**
	 * 修改数据
	 * 
	 * @param user
	 * @return
	 */
	public Result update(OrgUser user) {
		if (orgUserMapper.update(user) > 0) {
			return ResultUtil.success();
		} else {
			return ResultUtil.fail();
		}
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 * @return
	 */
	public Result delete(String id) {
		orgUserMapper.delete(id);
		return ResultUtil.success();
	}
	
	public Result login(String userId,HttpServletRequest request){
		OrgUser user = orgUserMapper.login(userId);
		Result result = null;
		if(user!=null){
			request.getSession().setAttribute("userinfo", user);
			result = ResultUtil.success("登录成功！");
		}else{
			result = ResultUtil.fail();
		}
		return result;
	}
	
	public Map<String,Object> get(){
		return orgUserMapper.get();
	}
}
